<?php
/**
 * Created by PhpStorm.
 * User: jx
 * Date: 2018-03-22
 * Time: 21:35
 */

namespace app\common\logic;
use think\Model;

use app\common\logic\Logic;
use app\common\model\Module;
use app\common\model\Type;
use think\Cache;
class TypeLogic extends Logic
{
    protected $modelClass = 'app\common\model\Type';
    protected $column = 'parentid,id,title,level';
    protected $order = 'listorder desc';
    
    public function getFoldList($len=1){
    
//        Cache::rm('typeFoldList');
        if(Cache::get('typeFoldList')){
            return Cache::get('typeFoldList');
        }
    
        $rt = [];
        $this->size = null;
        $this->field = 'id,title';
        $this->condition['parentid'] = 0;
        $one = $this->prepareDataProvider();
        
        //只要一级
        if($len==1){
            $rt = $one;
            Cache::set('typeFoldList',$rt);
            return $rt;
        }
        
        foreach ($one as $menu1){
            
            
            $this->condition['parentid'] = $menu1['id'];
            $two = $this->prepareDataProvider();
           
            $menu1['child'] = [];
            //只要二级
            if($len==2){
                $menu1['child'] = $two;
                $rt[] = $menu1;
                continue;
            }
            
            foreach ($two as $menu2){
                

                $this->condition['parentid'] = $menu2['id'];
                $menu2['child'] = [];
                $menu2['child'] = $this->prepareDataProvider();

                $menu1['child'] = $two;
            
            }
            
            $rt[] = $menu1;
        }
    
        Cache::set('typeFoldList',$rt);
        return $rt;
        
        
    }
    
    
    public function getLevel($parentid){
        if($parentid==0){
            return 1;
        }
        $parent = Type::get($parentid);
        if(!$parent||!$parent['level']){
            exception('父级类目没有级别信息');
        }
        return $parent['level']+1;
        
    }
    
    

    

}